/**
 * Copyright (c) 2023 murenchao
 * taomu is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.box.datasource

import com.google.inject.Provider
import com.google.inject.name.Named
import javax.inject.Inject
import javax.sql.DataSource
import org.apache.commons.dbcp2.BasicDataSource

class Dbcp2DataSourceProvider implements Provider<DataSource> {
	BasicDataSource dataSource = new BasicDataSource();

	@Inject
	def void setDirver(@Named("dbcp2.jdbc.driver") String driverCLass) {
		dataSource.driverClassName = driverCLass;
	}

	@Inject
	def void setJdbcUrl(@Named("dbcp2.jdbc.url") String url) {
		dataSource.setUrl = url;
	}

	@Inject
	def void setUname(@Named("dbcp2.jdbc.username") String uname) {
		dataSource.setUsername = uname;
	}

	@Inject
	def void setPasswd(@Named("dbcp2.jdbc.password") String pwd) {
		dataSource.setPassword = pwd;
	}
	
	@Inject
	def void autoCommit(@Named("dbcp2.autoCommit") boolean value) {
		dataSource.defaultAutoCommit = value;
	}
	
	@Inject
	def void readOnly(@Named("dbcp2.readOnly") boolean value) {
		dataSource.defaultReadOnly = value;
	}
	@Inject
	def void transactionIsolation(@Named("dbcp2.transactionIsolation") Integer value) {
		dataSource.defaultTransactionIsolation = value;
	}
	@Inject
	def void defaultCatalog(@Named("dbcp2.defaultCatalog") String value) {
		dataSource.defaultCatalog = value;
	}
	
	@Inject
	def void initialSize(@Named("dbcp2.initialSize") int value) {
		dataSource.initialSize = value;
	}
	@Inject
	def void maxActive(@Named("dbcp2.maxTotal") int value) {
		dataSource.maxTotal = value;
	}
	@Inject
	def void minIdle(@Named("dbcp2.minIdle") int value) {
		dataSource.minIdle = value;
	}
	@Inject
	def void maxIdle(@Named("dbcp2.maxIdle") int value) {
		dataSource.maxIdle = value;
	}
	@Inject
	def void maxWait(@Named("dbcp2.maxWaitMillis") long value) {
		dataSource.maxWaitMillis = value;
	}
	
	@Inject
	def void validationQuery(@Named("dbcp2.validationQuery") String value) {
		dataSource.validationQuery = value;
	}
	@Inject
	def void validationQueryTimeout(@Named("dbcp2.validationQueryTimeout") int value) {
		dataSource.validationQueryTimeout = value;
	}
	@Inject
	def void testOnBorrow(@Named("dbcp2.testOnBorrow") boolean value) {
		dataSource.testOnBorrow = value;
	}
	@Inject
	def void testOnReturn(@Named("dbcp2.testOnReturn") boolean value) {
		dataSource.testOnReturn = value;
	}
	@Inject
	def void testWhileIdle(@Named("dbcp2.testWhileIdle") boolean value) {
		dataSource.testWhileIdle = value;
	}
	@Inject
	def void poolPreparedStatements(@Named("dbcp2.poolPreparedStatements") boolean value) {
		dataSource.poolPreparedStatements = value;
	}
	
	@Inject
	def void accessToUnderlyingConnectionAllowed(@Named("dbcp2.accessToUnderlyingConnectionAllowed") boolean value) {
		dataSource.accessToUnderlyingConnectionAllowed = value;
	}
	@Inject
	def void timeBetweenEvictionRunsMillis(@Named("dbcp2.timeBetweenEvictionRunsMillis") long value) {
		dataSource.timeBetweenEvictionRunsMillis = value;
	}
	@Inject
	def void minEvictableIdleTimeMillis(@Named("dbcp2.minEvictableIdleTimeMillis") long value) {
		dataSource.minEvictableIdleTimeMillis = value;
	}
	
	@Inject
	def void logAbandoned(@Named("dbcp2.logAbandoned") boolean value) {
		dataSource.logAbandoned = value;
	}
	@Inject
	def void maxOpenPreparedStatements(@Named("dbcp2.maxOpenPreparedStatements") int value) {
		dataSource.maxOpenPreparedStatements = value;
	}

	def void configure(BasicDataSource dataSource) {
	}

	override get() {
		this.configure(this.dataSource);
		return dataSource;
	}
}
